<template>
	<view class="login uni-padding-wrap uni-common-mt">
		<view class="form form-a">
			<view class="form-group">
				<view class="form-item">
					<text class="form-item-header">账号:</text>
					<view class="form-item-body">
						<input type="text" class="form-control" placeholder="请输入账号" v-model="userName" />
					</view>
				</view>
				<view class="form-item end">
					<text class="form-item-header">密码:</text>
					<view class="form-item-body">
						<input type="password" class="form-control" placeholder="请输入密码" v-model="password" />
					</view>
				</view>
			</view>
		</view>

		<view class="btns">
			<view class="btns-group">
				<button class="btn" @tap="submit">登录</button>
			</view>
		</view>
	</view>
</template>
<script>
	import { login } from '@/api/configure.js';
	import { deepClone, getRegistrationID } from '@/util/util.js';
	import {Push} from '@/util/push.js';

	export default {
		data() {
			return {
				userName: '',
				password: ''
			}
		},
		methods: {
			async submit() {
				if (!this.userName) {
					uni.showToast({
						title: '用户名不能为空',
						duration: 2000,
						icon: 'none'
					});

					return
				}

				if (!this.password) {
					uni.showToast({
						title: '密码不能为空',
						duration: 2000,
						icon: 'none'
					});

					return
				}
				
				let pushId = this.$store.getters['push/getPushId'];
				
				let data = {
					username: this.userName,
					password: this.password,
					pushId
				}
				
				console.log(data);
				
				this.$store.dispatch({
					type: 'login',
					data: data
				}).then((userInfo) => {
					this.openPage();
					
					uni.$emit('uploadGps.check');
				})
			},
			openPage() {
				uni.navigateTo({
					url: '/pages/index/index'
				});
			}
		},
		onLoad() {},
		onReady() {
			let push = new Push({
				fn: (pushId) => {
					this.$store.dispatch({
						type: 'push/setPushId',
						pushId
					});
				}
			});
			
			push.cycle();
			
			let userName = uni.getStorageSync('userName');
			let password = uni.getStorageSync('password');

			if (userName && password) {
				this.userName = userName;
				this.password = password;
			} else {
				this.userName = '';
				this.password = '';
			}
		},
	}
</script>

<style lang="scss">
	.form {
		margin-bottom: 40rpx;
		position: relative;

		&:after {
			content: "";
			width: 200%;
			height: 200%;
			transform: scale(.5);
			transform-origin: 0 0;
			box-sizing: border-box;
			position: absolute;
			top: 0;
			left: 0;
			z-index: 2;
			border: 1rpx solid #f29004;
			border-radius: 20rpx;
			opacity: 0.382;
		}

		.form-group {
			position: relative;
			z-index: 3;

			.form-item {
				display: flex;
				padding: 18rpx 0rpx;
				position: relative;

				&:after {
					content: "";
					transform: scaleY(.5);
					position: absolute;
					bottom: 0;
					left: 0;
					right: 0;
					opacity: 0.382;
					height: 1rpx;
					background-color: #f29004;
				}

				.form-item-header {
					width: 100rpx;
					padding: 6rpx 20rpx;
					font-size: 24rpx;
					line-height: 32rpx;
					color: #5d5d5d;
				}

				.form-item-body {
					width: calc(100% - 140rpx);

					.form-control {
						padding: 6rpx 6rpx;
						font-size: 24rpx;
						line-height: 32rpx;
						height: 44rpx;
						color: #5d5d5d;
						box-sizing: border-box;
					}
				}

				&.end:after {
					display: none;
				}
			}
		}
	}

	.btns {
		.btns-group {}
	}

	.btn {
		padding: 20rpx 0px;
		background-color: #f29004;
		font-size: 28rpx;
		line-height: 120%;
		color: #ffffff;
	}
</style>
